home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo1.zoo / demo / msg / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  5.2 KB  |  225 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8.  
  9. /* convert current directory into a heirarchy of menus */
  10.  
  11. /*    $Header: menus.c,v 4.3 88/08/29 13:27:16 sau Exp $
  12.     $Source: /tmp/mgrsrc/demo/msg/RCS/menus.c,v $
  13. */
  14. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/msg/RCS/menus.c,v $$Revision: 4.3 $";
  15.  
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <sys/dir.h>
  19. #include <stdio.h>
  20. #include "term.h"
  21.  
  22. #define MAX        512                    /* max path length */
  23. #define MENU        1000                /* max menu buffer length */
  24. #define MENU_NAME    ".menus"            /* default command menu */
  25. #define MAX_ITEMS    20                    /* max items per menu */
  26. #define MAX_MENUS    9                    /* max # of paged menus */
  27.  
  28. /* menu names */
  29.  
  30. #define CMD_MENU    1
  31. #define MAIN_MENU    2
  32. #define FILE_MENU    3
  33. #define DIR_MENU    4
  34.  
  35. #define ARG        (argc>1 ? argv[1] : home)
  36. #define COPY(where,index,what) \
  37.     (index+strlen(what)<MENU ? \
  38.               strcpy(where+index,what), index += strlen(what) \
  39.         :     0)
  40.  
  41. static char buff[MAX];
  42. static char load[MENU];
  43. static char stat_buff[MAX];
  44. static int len;
  45.  
  46. static int type = S_IFDIR;    /* type of file to accept */
  47.  
  48. struct menu_entry main_menu[] = {
  49.     "Commands    =>", "",
  50.     "Files       =>", "",
  51.     "Directories =>", "",
  52.     };
  53.  
  54. struct menu_entry cmd_menu[] = {
  55.     "ls", "ls\r",
  56.     "pwd","pwd\r",
  57.     };
  58.  
  59. main(argc,argv)
  60. int argc;
  61. char **argv;
  62.    {
  63.    register int i, pos=0;
  64.    int file;
  65.    char **dir_list(),**list;
  66.     register char **chunk;
  67.    char *getenv(), *home = getenv("HOME");
  68.    char *sprintf();
  69.    char host[32];
  70.    char tmp[100];
  71.  
  72.     int dir_index = 0;            /* for pages dir menus */
  73.     int file_index = 0;            /* for paged file menus */
  74.  
  75.    ckmgrterm(*argv);
  76.     m_setup(M_DEBUG);
  77.  
  78.    /* see if hostname required */
  79.  
  80.    if (argc>1 && strcmp(argv[1],"-h")==0) {
  81.       gethostname(host,sizeof(host));
  82.       argc--, argv++;
  83.       }
  84.    else
  85.       *host = '\0';
  86.  
  87.     /* command menu (should be menu 1) */
  88.  
  89.    if ((file = open(sprintf(buff,"%s/%s",ARG,MENU_NAME),0)) > 0) {
  90.       while((i=read(file,buff,sizeof(buff))) > 0)
  91.          write(1,buff,i);
  92.       close(file);
  93.       }
  94.     else        /* load up default command menu */
  95.         menu_load(CMD_MENU,2,cmd_menu);
  96.  
  97.     menu_load(MAIN_MENU,3,main_menu);    /* load main menu */
  98.  
  99.     /* get directory menus */
  100.  
  101.     list = dir_list(ARG);
  102.    chunk = list;
  103.     while (chunk != (char **) 0) {
  104.         if (dir_index==0)
  105.             COPY(load,pos,"\005 ..");
  106.         for(i=0;i<MAX_ITEMS && chunk[i]!=NULL;i++) {
  107.             COPY(load,pos,"\005");
  108.             COPY(load,pos,chunk[i]);
  109.             }
  110.         if (dir_index==0)
  111.             COPY(load,pos,"\005cd ..\r");
  112.          for(i=0;i<MAX_ITEMS && chunk[i]!=NULL;i++) {
  113.             COPY(load,pos,"\005cd ");
  114.             COPY(load,pos,chunk[i]);
  115.             COPY(load,pos,"\r");
  116.             }
  117.        load[pos] = '\0';
  118.        m_loadmenu(DIR_MENU+dir_index,load);
  119.  
  120.         if (dir_index > 0)
  121.             m_pagemenu(DIR_MENU+dir_index-2, DIR_MENU+dir_index);
  122.         dir_index += 2;
  123.         if (chunk[i] && dir_index < MAX_MENUS*2)
  124.             chunk += i;
  125.         else 
  126.             break;
  127.         pos = 0;
  128.       }
  129.  
  130.     /* get plain file menus */
  131.  
  132.   pos = 0;
  133.   type = S_IFREG;
  134.   free(list);
  135.   list = dir_list(ARG);
  136.   chunk = list;
  137.   while (chunk != (char **) 0) {
  138.         for(i=0;i<MAX_ITEMS && chunk[i]!=NULL;i++) {        /* items */
  139.             COPY(load,pos,"\005");
  140.             COPY(load,pos,chunk[i]);
  141.             }
  142.         for(i=0;i<MAX_ITEMS && chunk[i]!=NULL;i++) {        /* actions */
  143.             if (*host)
  144.                 sprintf(tmp,"%s%c%s/%s",host,':',ARG,chunk[i]);
  145.             else
  146.                 sprintf(tmp,"%s/%s",ARG,chunk[i]);
  147.             COPY(load,pos,sprintf(buff,"\005echo %c%d%c",ESC,strlen(tmp)+1,E_SNARF));
  148.             COPY(load,pos,tmp);
  149.             COPY(load,pos,"\r");
  150.             }
  151.         load[pos] = '\0';
  152.         m_loadmenu(FILE_MENU+file_index,load);
  153.         if (file_index > 0)
  154.             m_pagemenu(FILE_MENU+file_index-2, FILE_MENU+file_index);
  155.         file_index += 2;
  156.         if (chunk[i] && file_index < MAX_MENUS*2)
  157.             chunk += i;
  158.         else 
  159.             break;
  160.         pos = 0;
  161.         }
  162.  
  163.     free(list);
  164.  
  165.     /* setup menu links */
  166.  
  167.     m_selectmenu(MAIN_MENU);
  168.     m_linkmenu(MAIN_MENU,0,1,0);
  169.     m_linkmenu(MAIN_MENU,1,FILE_MENU,0);
  170.     m_linkmenu(MAIN_MENU,2,DIR_MENU,0);
  171.     m_flush();
  172.    }
  173.  
  174. /* list all "right" files in a directory */
  175.  
  176. char **
  177. dir_list(path)
  178. char *path;
  179.    {
  180.    register int i;
  181.    struct direct **entries;
  182.    int select(), dir_sort();
  183.    int count;
  184.    char *malloc();
  185.    char **list;
  186.    
  187.    len = strlen(path);
  188.    strcpy(stat_buff,path);
  189.    if (stat_buff[len-1] != '/')
  190.       strcat(stat_buff,"/"),len++;
  191.    if ((count = scandir(path,&entries,select,dir_sort)) < 0)
  192.       return ((char **) 0);
  193.    list = (char **) malloc(sizeof(char *) *(count+1));
  194.    for(i=0;i<count;i++) list[i]=entries[i]->d_name;
  195.    list[count] = (char *) 0;
  196.    return(list);
  197.    }
  198.  
  199. #define SUFFIX(x,s) \
  200.     (strncmp(x + strlen(x) - strlen(s),s,strlen(s))==0)
  201. int
  202. select(d1)
  203. struct direct *d1;
  204.    {
  205.    struct stat buf;
  206.  
  207.    if (*d1->d_name != '.' && !SUFFIX(d1->d_name,".o")) {
  208.       strcat(stat_buff,d1->d_name);
  209.       stat(stat_buff,&buf);
  210.       stat_buff[len] = '\0';
  211.       if ((buf.st_mode&S_IFMT) == type)     /* directory */
  212.          return(1);
  213.       else
  214.          return(0);
  215.       }
  216.    return(0);
  217.    }
  218.  
  219. int
  220. dir_sort(d1,d2)
  221. register struct direct **d1, **d2;
  222.    {
  223.    return(strcmp((*d1)->d_name,(*d2)->d_name));
  224.    }
  225.